﻿using Dapper;
using ERPEF.Model;
using ERPService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data;

namespace S4Project.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Consumes("application/json")]

    // 基本资料：员工控制器
    public class EmployeeController : ControllerBase
    {
        private readonly bs_employeeService bs_Employee;
        private readonly MyDbContext myDbContext;

        public EmployeeController(MyDbContext myDb, bs_employeeService bs_EmployeeService)
        {
            myDbContext = myDb;
            bs_Employee = bs_EmployeeService;
        }

        //员工查询
        [HttpGet]
        public async Task<IActionResult> Query() 
        {
            var employee = await bs_Employee.Query();
            if (employee != null)
            {
                return Ok(employee);
            }
            else
            {
                return NotFound("404");
            }
        }

        //添加
        [HttpPost]
        public async Task<IActionResult> AddEmployee(bs_employee bs_employee)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_Employee.Add(bs_employee);
                if (result > 0)
                {
                    return Ok("添加成功！");
                }
                else
                {
                    return BadRequest("添加失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }
        //修改
        [HttpPut]
        public async Task<IActionResult> updateEmployee(bs_employee bs_employee)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_Employee.Update(bs_employee);
                if (result > 0)
                {
                    return Ok("修改成功！");
                }
                else
                {
                    return BadRequest("修改失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        //逻辑删除
        [HttpDelete("{employee_id}")]
        public async Task<IActionResult> DeleteEmployee(int employee_id)
        {
            int result = await bs_Employee.Delete(employee_id);
            if (result > 0)
            {
                return Ok("删除成功！");
            }
            else
            {
                return NotFound("未找到");
            }
        }

        /// <summary>
        /// 分页查询 
        /// </summary>
        /// <returns></returns> 
        /// 
        [HttpGet]
        public ActionResult<FenYe<bs_employee>> getFenYe(int currentPage, int pageSize, string where)
        {
            using (var conn = myDbContext.Database.GetDbConnection())
            {
                conn.Open();
                var parameters = new DynamicParameters();
                parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
                parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "employee_id"); // 更改为正确的列名
                parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "bs_employee"); // 更改为正确的表名
                parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
                parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where); // 添加 where 参数
                parameters.Add("row", dbType: DbType.Int32, direction: ParameterDirection.Output);  // 修改为和存储过程中一致的参数名
                string sql = "[dbo].[FenYe]";
                var result = conn.Query<bs_employee>(sql, parameters, commandType: CommandType.StoredProcedure);
                int row = parameters.Get<int>("row");
                FenYe<bs_employee> fenYe = new FenYe<bs_employee>()
                {
                    employeeData = result,
                    Rows = row,
                };

                if (fenYe.employeeData.Any())
                {
                    return fenYe;
                }
                else
                {
                    return Content("没有查到数据");
                }
            }
        }
    }
}
